package com.apchernykh.rozhlasapp.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.apchernykh.ceskyrozhlas.basic.R;
import com.apchernykh.types.ApplicationModel.EPlaybackStatuses;

public abstract class BaseDetailsFragment extends android.support.v4.app.Fragment {
	protected View mDetailedView = null;
	public static void memorizeIndex(int iIndex, Fragment iFragment) {
		// Supply index input as an argument.
		Bundle aArgs = new Bundle();
		aArgs.putInt("index", iIndex);
		iFragment.setArguments(aArgs);
	}

	public int getShownIndex() {
		if (getArguments() != null) {
			return getArguments().getInt("index", 0);
		} else {
			return -1;
		}
	}

	public static  void setOnPlayButtonListener(View aPlayButton) {
		aPlayButton
				.setOnClickListener(getListenerForPlaybackState(RozhlasAppBaseFragmentActivity.mApplicationModel.mBackgroundPlaybackService
						.getPlaybackStatus()));
		ImageView aIv = (ImageView) aPlayButton;
		if (aIv != null) {
			aIv.setImageResource(getImageIdForPlaybackState(RozhlasAppBaseFragmentActivity.mApplicationModel.mBackgroundPlaybackService
					.getPlaybackStatus()));
		}
	}

	public static int getImageIdForPlaybackState(EPlaybackStatuses iPlaybackStatus) {
		if (shouldShowStopButton(iPlaybackStatus)) {
			return R.drawable.btn_playback_ic_pause_small;
		} else {
			return R.drawable.ic_menu_play_clip;
		}
	}

	public static boolean shouldShowStopButton(EPlaybackStatuses iPlaybackStatus) {
		if (iPlaybackStatus == EPlaybackStatuses.STOPPED) {
			return false;
		}
		return true;
//		StationInfo aInfo = RozhlasAppBaseFragmentActivity.mApplicationModel.getSelectedStationInfo();
//		if (aInfo == null || aInfo.mShortName == null) {
//			return false;
//		}
//		DetailedUrl aUrl = RozhlasAppBaseFragmentActivity.mApplicationModel.mBackgroundPlaybackService.getExtendedPlaybackInfo();
//		if (aUrl == null || aUrl.mTitle == null) {
//			return false;
//		}
//		return aInfo.mShortName.equals(aUrl.mTitle);
	}
	
	public static OnClickListener getListenerForPlaybackState(EPlaybackStatuses iPlaybackStatus) {
		if (shouldShowStopButton(iPlaybackStatus)) {
			return RozhlasAppBaseFragmentActivity.mApplicationModel.onStopButtonListener;
		} else {
			return RozhlasAppBaseFragmentActivity.mApplicationModel.onPlayButtonListener;
		}
	}
}